package com.niit.controller;

import com.niit.pojo.Place;
import com.niit.service.PlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import java.util.List;

@Controller
public class PlaceController {
    @Autowired
    private PlaceService placeService;
    @RequestMapping("/toAddPlace")
    public String toAddPlace(){
        return "/addPlace";
    }

    @RequestMapping("addPlace")
    public String addPlace(Place place){
        placeService.addPlace(place);

        return "redirect:/toAddProduct";
    }

    @RequestMapping("/listPlace")
    public String listPlace(Model model){
        List<Place> placeList=placeService.selectAllPlace();
        for (Place place : placeList) {
            System.out.println(place);

        }
        model.addAttribute("placeList",placeList);
        return "/listPlace";
    }
    @RequestMapping("/deletePlace")
    public String deletePlace(@RequestParam("place_id") int place_id){
        placeService.deletePlaceById(place_id);
        return "redirect:/listPlace";

    }

    @RequestMapping("toUpdatePlace")
    public String toUpdatePlace(@RequestParam("place_id")int id,Model model){
        Place place=placeService.selectPlaceByName(id);
        System.out.println("toUpdatePlace"+place);
        model.addAttribute("place",place);
        return "/updatePlace";
    }

    @RequestMapping("/updatePlace")
    public String UpdatePlace(Place place){
        placeService.updatePlace(place);
        return "redirect:/listPlace";
    }

    @RequestMapping()
    public String updatePlace(Place place){
        placeService.updatePlace(place);
        return "redirect:/listPlace";
    }

}
